﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Business_Logic;
using Business_Entity;

namespace PetSoft.GUI.Seguridad
{
    public partial class GUIAdmRoles : Form
    {
        private Boolean esNuevo;
        private Boolean seEdita;
        private String idRol;

        public GUIAdmRoles(Boolean esNuevo)
        {
            this.esNuevo = esNuevo;
            InitializeComponent();
            this.idRol = "";

        }

        public void marcarChkBoxes(Boolean seMarca)
        {
            this.PRR.Checked = seMarca;
            this.PRM.Checked = seMarca;
            this.PCR.Checked = seMarca;
            this.PCM.Checked = seMarca;
            this.OCR.Checked = seMarca;
            this.OCM.Checked = seMarca;
            this.REC.Checked = seMarca;
            this.CMM.Checked = seMarca;
            this.CMR.Checked = seMarca;
            this.PVR.Checked = seMarca;
            this.PVM.Checked = seMarca;
            this.FCM.Checked = seMarca;
            this.FCR.Checked = seMarca;
            this.SVR.Checked = seMarca;
            this.SVM.Checked = seMarca;
            this.BSR.Checked = seMarca;
            this.BSM.Checked = seMarca;
            this.EMR.Checked = seMarca;
            this.EMM.Checked = seMarca;
            this.REA.Checked = seMarca;
            this.REM.Checked = seMarca;
            this.USM.Checked = seMarca;
            this.USR.Checked = seMarca;
            this.ROR.Checked = seMarca;
            this.ROM.Checked = seMarca;
            this.TIM.Checked = seMarca;
            this.TIR.Checked = seMarca;
            this.EPR.Checked = seMarca;
            this.EPM.Checked = seMarca;
            this.SRR.Checked = seMarca;
            this.SRM.Checked = seMarca;
            this.PRN.Enabled= seMarca; 
            this.DSC.Enabled= seMarca; 
            this.RVN.Enabled= seMarca;
            this.ADM.Enabled= seMarca;
            this.DMM.Enabled= seMarca; 
            this.RVR.Enabled= seMarca;
            this.UCR.Enabled= seMarca;
            this.UCM.Enabled= seMarca;
            this.VLR.Enabled= seMarca;
            this.VLM.Enabled= seMarca;
        }

        public void habilitarChkBoxes(Boolean seMarca)
        {
            this.PRR.Enabled = seMarca;
            this.PRM.Enabled = seMarca;
            this.PCR.Enabled = seMarca;
            this.PCM.Enabled = seMarca;
            this.OCR.Enabled = seMarca;
            this.OCM.Enabled = seMarca;
            this.REC.Enabled = seMarca;
            this.CMM.Enabled = seMarca;
            this.CMR.Enabled = seMarca;
            this.PVR.Enabled = seMarca;
            this.PVM.Enabled = seMarca;
            this.FCM.Enabled = seMarca;
            this.FCR.Enabled = seMarca;
            this.SVR.Enabled = seMarca;
            this.SVM.Enabled = seMarca;
            this.BSR.Enabled = seMarca;
            this.BSM.Enabled = seMarca;
            this.EMR.Enabled = seMarca;
            this.EMM.Enabled = seMarca;
            this.REA.Enabled = seMarca;
            this.REM.Enabled = seMarca;
            this.USM.Enabled = seMarca;
            this.USR.Enabled = seMarca;
            this.ROR.Enabled = seMarca;
            this.ROM.Enabled = seMarca;
            this.TIM.Enabled = seMarca;
            this.TIR.Enabled = seMarca;
            this.EPR.Enabled = seMarca;
            this.EPM.Enabled = seMarca;
            this.SRR.Enabled = seMarca;
            this.SRM.Enabled = seMarca;
            this.PRN.Enabled= seMarca; 
            this.DSC.Enabled= seMarca; 
            this.RVN.Enabled= seMarca;
            this.ADM.Enabled= seMarca;
            this.DMM.Enabled= seMarca; 
            this.RVR.Enabled= seMarca;
            this.UCR.Enabled= seMarca;
            this.UCM.Enabled= seMarca;
            this.VLR.Enabled= seMarca;
            this.VLM.Enabled= seMarca;
            
            
        }

        public GUIAdmRoles(Boolean seEdita, String idRol)
        {
            this.seEdita = esNuevo;
            this.idRol = idRol;

            InitializeComponent();
            this.RolTxt.ReadOnly = true;
            this.RolTxt.Text = this.idRol;

            BLRol blRol = new BLRol();
            this.DescripcionTxt.Text = blRol.buscarDescripcion(this.idRol);

            marcarChkBoxes(false);
            marcarPrivilegios(this.idRol);
            if (!seEdita)
                habilitarChkBoxes(false);
        }




        public void marcarPrivilegios(string idRol)
        {


            List<BEPrivilegio> listaPrivs = new List<BEPrivilegio>();

            listaPrivs = (new BLPrivilegio()).getPrivs(idRol);

            for (int i = 0; i < listaPrivs.Count; i++)
            {
                if (listaPrivs[i].Modulo.Equals("COMP"))
                {
                    if (listaPrivs[i].Ventana.Equals("PROV"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.PRR.Checked = true;
                        if (listaPrivs[i].IdPrivilegio == 2)
                            this.PRM.Checked = true;
                    }


                    if (listaPrivs[i].Ventana.Equals("PEDC"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.PCR.Checked = true;
                        if (listaPrivs[i].IdPrivilegio == 2)
                            this.PCM.Checked = true;
                        if (listaPrivs[i].IdPrivilegio == 3)
                            this.PRN.Checked = true;
                    }


                    if (listaPrivs[i].Ventana.Equals("ORDC"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.OCR.Checked = true;
                        if (listaPrivs[i].IdPrivilegio == 2)
                            this.OCM.Checked = true;
                    }


                    if (listaPrivs[i].Ventana.Equals("REPC"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 3)
                            this.REC.Checked = true;
                    }
                }

                if (listaPrivs[i].Modulo.Equals("VENT"))
                {
                    if (listaPrivs[i].Ventana.Equals("CLMA"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.CMR.Checked = true;
                        if (listaPrivs[i].IdPrivilegio == 2)
                            this.CMM.Checked = true;
                    }


                    if (listaPrivs[i].Ventana.Equals("PEDV"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.PVR.Checked = true;
                        if (listaPrivs[i].IdPrivilegio == 2)
                            this.PVM.Checked = true;
                    }


                    if (listaPrivs[i].Ventana.Equals("FLUC"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.FCR.Checked = true;
                        if (listaPrivs[i].IdPrivilegio == 2)
                            this.FCM.Checked = true;
                    }
                    
                     if (listaPrivs[i].Ventana.Equals("DESC"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.DSC.Checked = true;
                    }
                     
                      if (listaPrivs[i].Ventana.Equals("REPV"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 3)
                            this.RVN.Checked = true;
                       
                    }

                }


                if (listaPrivs[i].Modulo.Equals("SERV"))
                {
                    if (listaPrivs[i].Ventana.Equals("SERV"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.SVR.Checked = true;
                        if (listaPrivs[i].IdPrivilegio == 2)
                            this.SVM.Checked = true;
                    }

                    if (listaPrivs[i].Ventana.Equals("BUSP"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.BSR.Checked = true;
                    }
                    
                     if (listaPrivs[i].Ventana.Equals("ADOP"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.ADM.Checked = true;
                       
                    }
                      if (listaPrivs[i].Ventana.Equals("DADO"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.DMM.Checked = true;
                       
                    }
                    
                    

                }

                if (listaPrivs[i].Modulo.Equals("RRHH"))
                {
                    if (listaPrivs[i].Ventana.Equals("EMPL"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.EMR.Checked = true;
                        if (listaPrivs[i].IdPrivilegio == 2)
                            this.EMM.Checked = true;
                    }


                    if (listaPrivs[i].Ventana.Equals("REPA"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 3)
                            this.REA.Checked = true;
                    }

                   

                    if (listaPrivs[i].Ventana.Equals("REPE"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 3)
                            this.REM.Checked = true;
                    }

                }

                if (listaPrivs[i].Modulo.Equals("GREC"))
                {
                    if (listaPrivs[i].Ventana.Equals("PROD"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.PDR.Checked = true;
                        if (listaPrivs[i].IdPrivilegio == 2)
                            this.PDM.Checked = true;
                    }


                    if (listaPrivs[i].Ventana.Equals("PEDP"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.PPR.Checked = true;
                        if (listaPrivs[i].IdPrivilegio == 2)
                            this.PPM.Checked = true;
                    }


                    if (listaPrivs[i].Ventana.Equals("DESP"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.DPR.Checked = true;
                        if (listaPrivs[i].IdPrivilegio == 2)
                            this.DPM.Checked = true;
                    }

                    if (listaPrivs[i].Ventana.Equals("ESPP"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.EPR.Checked = true;
                        if (listaPrivs[i].IdPrivilegio == 2)
                            this.EPM.Checked = true;
                    }

                    if (listaPrivs[i].Ventana.Equals("SERV"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.SRR.Checked = true;
                        if (listaPrivs[i].IdPrivilegio == 2)
                            this.SRM.Checked = true;
                    }
                    
                    if (listaPrivs[i].Ventana.Equals("REPG"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 3)
                            this.RVR.Checked = true;
                      
                    }

                }

                if (listaPrivs[i].Modulo.Equals("SEGU"))
                {
                    if (listaPrivs[i].Ventana.Equals("USUA"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.USR.Checked = true;
                        if (listaPrivs[i].IdPrivilegio == 2)
                            this.USM.Checked = true;
                    }
					
                    if (listaPrivs[i].Ventana.Equals("USUC"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.UCR.Checked = true;
                        if (listaPrivs[i].IdPrivilegio == 2)
                            this.UCM.Checked = true;
                    }

                    if (listaPrivs[i].Ventana.Equals("ROLE"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.ROR.Checked = true;
                        if (listaPrivs[i].IdPrivilegio == 2)
                            this.ROM.Checked = true;
                    }
                }

                if (listaPrivs[i].Modulo.Equals("CONF"))
                {
                    if (listaPrivs[i].Ventana.Equals("TIEN"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.TIR.Checked = true;
                        if (listaPrivs[i].IdPrivilegio == 2)
                            this.TIM.Checked = true;
                    }
                    if (listaPrivs[i].Ventana.Equals("VALO"))
                    {
                        if (listaPrivs[i].IdPrivilegio == 1)
                            this.VLR.Checked = true;
                        if (listaPrivs[i].IdPrivilegio == 2)
                            this.VLM.Checked = true;
                    }
                }
            }
        }


        private void BuscarUsuarioBTN_Click(object sender, EventArgs e)
        {
            GUIBuscarUsuario buscarUsuario = new GUIBuscarUsuario();
            buscarUsuario.Show();
        }

        public void Button1Click(object sender, EventArgs e)
        {
            int result = 0;
            if (esNuevo)
            {

                if ((MessageBox.Show("¿Está seguro que desea guardar el nuevo registro?", "Confirmación", MessageBoxButtons.YesNo) == DialogResult.Yes))
                {
                    BLRol rol = new BLRol();
                    result = rol.guardaRol(this.RolTxt.Text, this.DescripcionTxt.Text, this.PRR.Checked, this.PRM.Checked, this.PCR.Checked, this.PCM.Checked, this.OCR.Checked, this.OCM.Checked, this.REC.Checked,
                              this.CMR.Checked, this.CMM.Checked, this.PVR.Checked, this.PVM.Checked, this.FCR.Checked, this.FCM.Checked, this.SVR.Checked, this.SVM.Checked, this.BSR.Checked, this.BSM.Checked,
                              this.EMR.Checked, this.EMM.Checked, this.REA.Checked, this.REM.Checked, this.PDR.Checked, this.PDM.Checked,
                              this.PPR.Checked, this.PPM.Checked, this.DPR.Checked, this.DPM.Checked, this.USR.Checked, this.USM.Checked, this.ROR.Checked, this.ROM.Checked,
                              this.TIR.Checked, this.TIM.Checked, this.EPR.Checked, this.EPM.Checked, this.SRR.Checked, this.SRM.Checked,
                              this.PRN.Checked, this.DSC.Checked, this.RVN.Checked, this.ADM.Checked, this.DMM.Checked, this.RVR.Checked,this.UCR.Checked, this.UCM.Checked, this.VLR.Checked, this.VLM.Checked);

                    if (result == 1)
                        Utils.MessageAviso("Nuevo rol registrado");
                    else if (result == 2)
                        Utils.MessageError("Rol ya existe");
                    else
                        Utils.MessageError("Error de registro de nuevo rol");
                }
            }
            else if (Utils.MessagePregunta("¿Está seguro que desea guardar los cambios?"))
            {
                BLRol blRol = new BLRol();
                
                result = blRol.guardaCambio(this.RolTxt.Text, this.DescripcionTxt.Text, this.PRR.Checked, this.PRM.Checked, this.PCR.Checked, this.PCM.Checked, this.OCR.Checked, this.OCM.Checked, this.REC.Checked,
                          this.CMR.Checked, this.CMM.Checked, this.PVR.Checked, this.PVM.Checked, this.FCR.Checked, this.FCM.Checked, this.SVR.Checked, this.SVM.Checked,
                          this.BSR.Checked, this.BSM.Checked, this.EMR.Checked, this.EMM.Checked, this.REA.Checked, this.REM.Checked, this.PDR.Checked, this.PDM.Checked,
                          this.PPR.Checked, this.PPM.Checked, this.DPR.Checked, this.DPM.Checked, this.USR.Checked, this.USM.Checked, this.ROR.Checked, this.ROM.Checked,
                          this.TIR.Checked, this.TIM.Checked, this.EPR.Checked, this.EPM.Checked, this.SRR.Checked, this.SRM.Checked,
                          this.PRN.Checked, this.DSC.Checked, this.RVN.Checked, this.ADM.Checked, this.DMM.Checked, this.RVR.Checked,this.UCR.Checked, this.UCM.Checked, this.VLR.Checked, this.VLM.Checked);
                if (result == 1)
                    Utils.MessageAviso("El rol " + this.RolTxt.Text + " ha sido modificado.");
                else if (result == 2)
                    Utils.MessageError("Rol ya existe");
                else
                    Utils.MessageError("Error en la modificacion del rol");
                
            }
            //return result;
        }
         
        
           
   
       
        public void EditarBTNClick(object sender, EventArgs e)
        {
            habilitarChkBoxes(true);
        }


        

        
        void Button2Click(object sender, EventArgs e)
        {
        	this.Close();
        }
    }}

